import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'cp-user',
  () => {
    // 用户仓库
    const user = ref<User>()
    // 修改用户信息
    const setUser = (u: User) => (user.value = u)
    // 删除用户信息
    const delUser = () => (user.value = undefined)

    // --------------
    // 登录成功后回跳地址
    const returnUrl = ref('')
    const setReturnUrl = (url: string) => (returnUrl.value = url)

    return { user, setUser, delUser, returnUrl, setReturnUrl }
  },
  {
    // 往本地存储一份, 默认是用localStorage
    persist: true
  }
)
